package com.cms.cinema.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;
import java.util.Objects;
import java.util.Vector;

/**
 * FileUtils
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-03-24 9:47
 * @blame Team
 */
@Slf4j
public class FileUtils {

    public static synchronized Vector<String> fileLineReader(String fileName){
        Objects.requireNonNull(fileName);
        Vector<String> stringVector = new Vector<>();
        try(
                //打开带读取的文件
                FileReader reader = new FileReader(fileName);
                BufferedReader br = new BufferedReader(reader);
        ){
            String line = null;
            while((line = br.readLine()) != null) {
                stringVector.add(line);
            }
        }catch (Exception ex){
            log.error("", ex);
        }
        return stringVector;
    }

    public static synchronized void fileLineWriter(List<String> lineList, String fileName){
        Objects.requireNonNull(lineList);
        Objects.requireNonNull(fileName);
        try(
                OutputStream os = new FileOutputStream(fileName);
                PrintWriter pw = new PrintWriter(os);
        ){
            lineList.forEach(pw::println);
        }catch (Exception ex){
            log.error("", ex);
        }
    }
}
